package functionalEnrichment.semanticSimilarity.semanticSimilarityCalculator.units;

import org.apache.commons.collections.FastHashMap;

/**
 * Contains a fast hash map of all genes. This class should be used as a repository for Gene
 * objects, so that multiple instances of equivalent genes are not created inorder to minimise
 * memory overhead.
 * @author Jamie MacPherson
 */
public class GeneLibrary {

    // the hash map of genes. A fast hashmap is used as the major time-consuming parts in this
    // application only access the hash, not add genes to it.
    private final FastHashMap genes = new FastHashMap();


    /**
     * Given a gene ID the library will create and return a new gene if it does not contain the
     * gene in its gene map already, otherwise a previously instantiated gene is returned.
     * @param id
     * @return
     */
    public Gene getGene(String id){
        if(!genes.containsKey(id)){
            Gene g = new Gene(id);
            genes.put(id, g);
            return g;
        } else {
            return (Gene) genes.get(id);
        }
    }

}
